//
//  NewsDetailViewController.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "UserInfo.h"
#import "LoginViewController.h"
#import "FileHandle.h"
#import "NewsModel.h"

#import <MBProgressHUD.h>
#import "MBProgressHUD+MJ.h"

@interface NewsDetailViewController ()<UserInfoDelegate, MBProgressHUDDelegate>

@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong)UserInfo *userInfo;
@property (nonatomic, strong)MBProgressHUD *HUD;
@end

@implementation NewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self webView];
    [self addLoadHUD];
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:_webView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self changeNavigationItem];
}

// 返回按钮 收藏按钮
- (void)changeNavigationItem
{
    // 返回
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_chbackbtn"] style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-shoucang"] style:(UIBarButtonItemStylePlain) target:self action:@selector(collect)];
    
    // 根据是否收藏改变红心颜色
    if (self.newsModel.isCollect) {
        [self selected];
    }else{
        [self select];
    }
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
}

- (void)select
{
    UIImage *select = [UIImage imageNamed:@"iconfont-shoucang (1)"];
    // 不渲染
    select = [select imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationItem.rightBarButtonItem setImage:select];
}

- (void)selected
{
    UIImage *selected = [UIImage imageNamed:@"iconfont-shoucang"];
    // 不渲染
    selected = [selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationItem.rightBarButtonItem setImage:selected];
}

- (UIWebView *)webView
{
    if (_webView == nil) {
        _webView = [[UIWebView alloc]initWithFrame:(CGRectMake(0, -44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height + 44))];
        NSString *str = _newsModel.url_3w;
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
        [_webView loadRequest:request];
        _webView.scrollView.bounces = NO;
    }
    return _webView;
}

- (void)back
{
    [self.webView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

// 清除web缓存
- (void)viewDidDisappear:(BOOL)animated
{
    // 清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
    NSURLCache *cahce = [NSURLCache sharedURLCache];
    [cahce removeAllCachedResponses];
    [cahce setDiskCapacity:0];
    [cahce setMemoryCapacity:0];
}

- (void)collect
{
    _userInfo = [UserInfo shareUserInfo];
    _userInfo.delegate = self;
    if (NO == _userInfo.isLogin) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }else{
        [self collectNews];
    }
}

- (void)collectNews
{
    // 1.用户登陆成功后 执行回调方法 把该活动添加到数据库
    // 添加之前需要对该活动进行判断 如果已经添加 提示已经添加 没有添加 则插入到数据库 并且把 activity 的添加标识改为 yes
    
    // 在数据库中 根据 id 查找是否存在
    // 已经收藏 提示
    if ([[FileHandle fileHandle]foundNewsForID:self.newsModel.docid]) {
        _newsModel.isCollect = NO;
        [self performSelector:@selector(showHUD) withObject:nil afterDelay:0.5];
        [self select];

        [[FileHandle fileHandle]removeNewsForID:self.newsModel.docid];
    }else{
        //未收藏， 添加到数据库
        _newsModel.isCollect = YES;
        [self performSelector:@selector(showHUD) withObject:nil afterDelay:0.5];
        [self selected];
        NSDictionary *myDic = @{
                                @"title":self.newsModel.title,@"url_3w":self.newsModel.url_3w,@"replyCount":self.newsModel.replyCount,@"digest":self.newsModel.digest,@"imgsrc":self.newsModel.imgsrc,@"docid":self.newsModel.docid
                                };
        [[FileHandle fileHandle]addNewsInFileDic:myDic ID:self.newsModel.docid];
    }
}

- (void)hideAlertView:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)showHUD
{
    if (self.newsModel.isCollect) {
        [MBProgressHUD showSuccess:@"收藏成功!"];
    }else{
        [MBProgressHUD showSuccess:@"取消收藏成功!"];
    }
}


#pragma mark - 加载转设置

// 添加加载转
- (void)addLoadHUD
{
    // 加载转
    _HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.navigationController.view addSubview:_HUD];
    _HUD.delegate = self;
    _HUD.labelText = @"正在加载……";
    [_HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}


// 加载时间为4秒
- (void)myTask {
    
    sleep(3);
}

// 加载 alert 消失
- (void)hudWasHidden:(MBProgressHUD *)hud {
    // Remove HUD from screen when the HUD was hidded
    [_HUD removeFromSuperview];
    _HUD = nil;
}

- (void)userInfo:(UserInfo *)userInfo select:(BOOL)isLogin
{
    [self collectNews];
}



@end
